#encoding:utf-8
from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from exts import db
from czbbs import create_app
from apps.cms import models as cms_models
from apps.front import models as front_models
from apps.models import BannerModel,BoardModel,PostModel,CommentModel

CMSUer = cms_models.CMSUser
CMSRole = cms_models.CMSRole
CMSPersmission = cms_models.CMSPersmission
FrontUser = front_models.FrontUser
app = create_app()
manager = Manager(app)

Migrate(app,db)

manager.add_command('db',MigrateCommand)

@manager.option('-u','--username',dest='username')
@manager.option('-p','--password',dest='password')
@manager.option('-e','--email',dest='email')
def create_cms_user(username,password,email):
    user = CMSUer(username=username,password=password,email=email)
    db.session.add(user)
    db.session.commit()
    print('cms用户添加成功')

@manager.command
def create_role():
    # 1、访问者，可以修改自己个人数据
    visitor = CMSRole(name='visitor',desc='just visitor')
    visitor.permissions = CMSPersmission.VISITOR
    # 2、运营角色（修改个人作息，管理帖子，管理评论，管理前台用户）
    operator = CMSRole(name='operator',desc='can delete update add')
    operator.permissions = CMSPersmission.VISITOR|CMSPersmission.CMSUSER|CMSPersmission.FRONTUSER\
    |CMSPersmission.POSTER|CMSPersmission.COMMENTER
    # 3、管理员（拥有绝大部份权限）
    admin = CMSRole(name='admin',desc='all the power')
    admin.permissions = CMSPersmission.COMMENTER|CMSPersmission.FRONTUSER|CMSPersmission.POSTER|CMSPersmission.VISITOR\
    |CMSPersmission.ADMINER|CMSPersmission.BOARDER|CMSPersmission.CMSUSER
    # 4、开发者权限
    developer = CMSRole(name='developer',desc='the super man who can do anything')
    developer.permissions = CMSPersmission.ALL_PERMISSION

    db.session.add_all([visitor,operator,admin,developer])
    db.session.commit()

@manager.option('-e','--email',dest='email')
@manager.option('-n','--name',dest='name')
def add_user_to_role(email,name):
    user = CMSUer.query.filter_by(email=email).first()
    if user:
        role = CMSRole.query.filter_by(name=name).first()
        if role:
            role.users.append(user)
            db.session.commit()
            print('用户添加到角色成功！')
        else:
            print('没有这个角色%s' % name)
    else:
        print('%s邮箱，没有这个用户！' % email)

@manager.option('-u','--username',dest='username')
@manager.option('-t','--telephone',dest='telephone')
@manager.option('-p','--password',dest='password')
def create_front_user(username,telephone,password):
    user = FrontUser(username=username,telephone=telephone,password=password)
    db.session.add(user)
    db.session.commit()


@manager.command
def text_permission():
    user = CMSUer.query.first()
    if user.has_permission(CMSPersmission.VISITOR):
        print('这个用户拥有访问者权限')
    else:
        print('这个用户没有访问者权限')


@manager.command
def create_text_post():
    for x in range(1,200):
        title = '标题%s' % x
        content = '内容%s' % x
        board = BoardModel.query.first()
        author = FrontUser.query.first()
        post = PostModel(title=title,content=content)
        post.board = board
        post.author = author
        db.session.add(post)
        db.session.commit()
    print('测试贴子添加成功')

if __name__ == '__main__':
    manager.run()
